﻿Imports System.Data.OleDb

Public Class maintenancePostsAdd

    Inherits System.Web.UI.Page
    Protected Sub btnmaintenancepost_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnmaintenancepost.Click
        If txtmaintenancepost.Text = "" Then
            MsgBox("You have not entered a post, please check and try again")

        Else
            Dim sql As String = "INSERT INTO Maintenance_Posts (PostBody,DatePosted,TimePosted,UserIDFK,MaintenanceDisIDFK) VALUES (@f1,@f2,@f3,@f4,@f5)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", txtmaintenancepost.Text)
            cmd.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
            cmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))
            cmd.Parameters.AddWithValue("@f4", Membership.GetUser(User.Identity.Name).ProviderUserKey)
            cmd.Parameters.AddWithValue("@f5", Request.QueryString("id"))

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            Response.Redirect("maintenancePostsAdd.aspx?id=" & Request.QueryString("id"))
        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        Dim sql5 As String = "SELECT * FROM AverageRatingsByMaintenancePosts WHERE MaintenanceDiscussionID=@f1"

        Dim conn5 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        conn5.Open()

        Dim cmd5 As OleDbCommand = New OleDbCommand(sql5, conn5)

        cmd5.Parameters.AddWithValue("@f1", Request.QueryString("id"))

        Dim dataReader = cmd5.ExecuteReader

        dataReader.Read()



        Try
            If Not IsDBNull(dataReader("AvgOfRating")) Then lblAverageRatings.Text = dataReader("AvgOfRating")
        Catch ex As Exception
            lblAverageRatings.Text = 0
        End Try


        If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then

        Else
            Response.Redirect("maintenancePostsAdd.aspx")
        End If


        Dim int As Single = CInt(lblAverageRatings.Text)


        If int >= 3 Then
            imgSmileyFace.Visible = True
            imgAngryFace.Visible = False

        Else
            imgAngryFace.Visible = True
            imgSmileyFace.Visible = False
        End If



    End Sub

    Protected Sub btnRate_Click(sender As Object, e As EventArgs) Handles btnRate.Click
        Dim sql3 As String = "DELETE FROM UserRatingMaintenancePosts WHERE Username=@f1 AND MaintenanceDiscussionID=@f2"
        Dim conn3 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn3.Open()

        Dim cmd3 As OleDbCommand = New OleDbCommand(sql3, conn3)

        cmd3.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd3.Parameters.AddWithValue("@f3", Request.QueryString("id"))

        cmd3.ExecuteNonQuery()
        conn3.Close()
        cmd3.Dispose()
        conn3.Dispose()









        Dim sql2 As String = "INSERT INTO UserRatingMaintenancePosts (Rating,Username,MaintenanceDiscussionID) VALUES (@f1,@f2,@f3)"

        Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        conn2.Open()

        Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn2)

        cmd2.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd2.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd2.Parameters.AddWithValue("@f3", Request.QueryString("id"))

        cmd2.ExecuteNonQuery()
        conn2.Close()
        cmd2.Dispose()
        conn2.Dispose()

        Response.Redirect("maintenancePostsAdd.aspx?id=" & Request.QueryString("id"))
    End Sub
End Class